package org.jeecg.modules.basedata.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.entity.basedata.BaseArea;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
* @Description: 远程调用演示服务， 供工程模块调用演示
* @Author: jay
* @Date:   2019-07-05
* @Version: V1.0
*/
@Slf4j
@Api(tags="远程调用演示服务端")
@RestController
@RequestMapping("/b/api/demo")
public class DemoServerController {


   @AutoLog(value = "演示服务端-简单参数")
   @ApiOperation(value="演示服务端", notes="演示服务端")
   @PostMapping(value = "/simple")
   public Result<Integer> simple(
           @RequestParam(name="op",defaultValue="add") String op,
           @RequestParam(name="arg1", defaultValue="0") Integer arg1,
           @RequestParam(name="arg2", defaultValue="0") Integer arg2) {
       switch (op) {
           case "add": return Result.ok(arg1 + arg2);
           case "sub": return Result.ok(arg1 - arg2);
           case "mul": return Result.ok(arg1 * arg2);
           case "div": return arg2 != 0 ? Result.ok(arg1 / arg2) : Result.error(10,"arg2 must not 0 :" + arg2);
           default: return Result.error(11,"operation must in (add|sub|mul|div) but get " + op);
       }
   }

    @AutoLog(value = "演示服务端-复杂对象")
    @ApiOperation(value="演示服务端", notes="演示服务端")
    @PostMapping(value = "/hight")
    public Result<Object> hight(
            @RequestBody MyDate data) {
       return Result.ok( data);
    }
    @Data
    public static class MyDate{
        BaseArea innerObj;
        String arg1;
        String arg2;
    }


    @RequestMapping(value = "/myTest")
    public static Result<String> myTest(
             String token,
             String data) {
        System.out.println("token："+token);
        System.out.println("data："+data);
        JSONArray resultData = new JSONArray();

        for (int i =1 ;i < 11 ;i++){
            JSONObject equip1 = new JSONObject();
            equip1.put("equipCode","code"+i);
            JSONArray itemArray = new JSONArray();
            for (int j = 1;j < 4; j++){
                JSONObject json = new JSONObject();
                json.put("item","000"+j);
                json.put("value","11"+j);
                json.put("time",new Date());
                itemArray.add(json);
            }
            equip1.put("dataItem",itemArray);
            resultData.add(equip1);
        }
        log.info("最终返回的数据为="+resultData.toJSONString());
        return Result.ok(resultData.toJSONString());
    }

    public static void main(String[] args) {
        myTest("11","11");
    }
}
